package cn.strive.eams.audit.service.impl;

import cn.strive.eams.application.mapper.ApplicationMapper;
import cn.strive.eams.application.pojo.entity.Application;
import cn.strive.eams.application.service.ApplicationService;
import cn.strive.eams.audit.mapper.AuditMapper;
import cn.strive.eams.audit.pojo.dto.AuditPassParam;
import cn.strive.eams.audit.pojo.dto.AuditQuery;
import cn.strive.eams.audit.pojo.dto.AuditSaveParam;
import cn.strive.eams.audit.pojo.entity.Audit;
import cn.strive.eams.audit.pojo.vo.AuditVO;
import cn.strive.eams.audit.service.AuditService;
import cn.strive.eams.base.enmus.ApplicationStatusEnum;
import cn.strive.eams.base.enmus.AuditStatusEnum;
import cn.strive.eams.busRequire.pojo.vo.BusRequireVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AuditServiceImpl implements AuditService {
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    ApplicationMapper applicationMapper;
    @Override
    public void insert(AuditSaveParam auditSaveParam) {
        log.debug("新增审批单,参数:{}", auditSaveParam);
        List<Long> userIdList = auditSaveParam.getAuditUserIdList();
        for (int i = 0; i < userIdList.size(); i++) {
            Audit audit = new Audit();
            audit.setApplyId(auditSaveParam.getApplyId());
            audit.setApplicationId(auditSaveParam.getApplicationId());
            audit.setApprovalId(userIdList.get(i));
            audit.setSort(i);
            if (i == 0) {
                audit.setStatus(AuditStatusEnum.MY_PENDING.getCode());
            } else {
                audit.setStatus(AuditStatusEnum.PENDING.getCode());
            }
            audit.setCreateTime(new Date());
            auditMapper.insert(audit);
        }
    }

    @Override
    public List<AuditVO> selectAll(AuditQuery auditQuery) {
       log.debug("查询审批项:{}", auditQuery);
        return auditMapper.selectAll(auditQuery);
    }

    @Override
    public List<BusRequireVO> selectBusRequire(Long auditId) {
        log.debug("查询申请项:{}", auditId);
        return auditMapper.selectBusRequire(auditId);
    }

/**
 * 处理审批通过的业务逻辑
 *
 * @param auditPassParam 审批通过参数对象，包含需要的审批信息
 */
@Override
public void busRequirePass(AuditPassParam auditPassParam) {

   Audit audit = new Audit();
   BeanUtils.copyProperties(auditPassParam, audit);
   log.debug("当前审批单信息内容:{}", audit);
   audit.setUpdateTime(new Date());
   auditMapper.update(audit);

   // 更新对应的申请单
   Application application = new Application();
   application.setId(audit.getApplicationId());
   application.setUpdateTime(new Date());

   // 判断审批状态,通过还是驳回
   if (auditPassParam.getStatus().equals(AuditStatusEnum.AUDITED.getCode())) {
       AuditQuery auditQuery = new AuditQuery();
       auditQuery.setApplicationId(auditPassParam.getApplicationId());
       Integer count = auditMapper.selectRestAuditCount(auditQuery);
       log.info("获取未审批单总数:{}", count);
       if (count > 0) {

           auditQuery.setAuditSort(auditPassParam.getSort()+1);
           log.debug("下一个审批单的顺序sort={}", auditPassParam.getSort()+1);

           List<AuditVO> auditVOList = auditMapper.selectAll(auditQuery);
           log.info("获取所有的未审批单:{}",auditVOList );
           if(auditVOList != null && auditVOList.size()>0){
               AuditVO auditVO1 = auditVOList.get(0);
               Audit Audit2 = new Audit();
               Audit2.setAuditId(auditVO1.getAuditId());
               Audit2.setStatus(AuditStatusEnum.MY_PENDING.getCode());
               Audit2.setUpdateTime(new Date());
               log.debug("更新审批单:{}", Audit2);
               auditMapper.update(Audit2);
           }
           application.setStatus(ApplicationStatusEnum.AUDIT.getCode());
           applicationMapper.update(application);
       } else {
           application.setStatus(ApplicationStatusEnum.AUDITED.getCode());
           applicationMapper.update(application);
       }
   } else if (auditPassParam.getStatus().equals(AuditStatusEnum.REJECT.getCode())){
      // 更新当前审批单状态为驳回
       auditMapper.update(audit);
       // 继续查其他审批单:其他审批单与当前审批单批的是同一个申请单
       AuditQuery auditQuery = new AuditQuery();
       auditQuery.setApplicationId(auditPassParam.getApplicationId());
       List<AuditVO> auditVOList = auditMapper.selectAll(auditQuery);
       log.info("获取所有的审批单:{}",auditVOList );
       if(auditVOList !=null && auditVOList.size()>0){
           for (AuditVO auditVO : auditVOList) {
               if (auditVO.getAuditStatus().equals(AuditStatusEnum.PENDING.getCode())) {
                   log.debug("删除审批单:{}", auditVO);
                   auditMapper.deleteById(auditVO.getAuditId());
               }
           }
       }
       //更新对应的申请单数据
       application.setStatus(ApplicationStatusEnum.REJECT.getCode());
       application.setRejectReason(auditPassParam.getRejectReason());
       applicationMapper.update(application);
   }
   }
}

